var basicForm = {
	allFields : null,
	init:function(){
		commentForm.allFields = $( [] ).add($("#movieComment"));
		$("#editBasic").bind("click", function() {
    		$("#editBasicDialog").dialog("open");

        });
		
		$( "#editBasicDialog" ).dialog({
			autoOpen: false,			
			height: 520,
			width: 800,
			modal: true,
			close: function() {					
				$(".validateTips").text( "" ).removeClass( "ui-state-highlight" );						
				commentForm.allFields.removeClass( "ui-state-error" );			
			},
			
			buttons: [
			   { text: "保存并转发到微博",
				 click: function() {
					$( "#sendToWeibo" ).val(true);
			        commentForm.allFields.removeClass( "ui-state-error" );
			        var bValid = plotForm.checkLength( $( "#movieComment" ), "请填写评论，长度1-140个字符。" );
					if ( bValid ) {
						$("#commentForm")[0].submit();							 							
						$( this ).dialog( "close" );
					} 
				  }
			    },
			    { 	text: "保存",
					 click: function() {
						$( "#sendToWeibo" ).val(false);
				        commentForm.allFields.removeClass( "ui-state-error" );
				        var bValid = plotForm.checkLength( $( "#movieComment" ), "请填写评论，长度1-1000个字符。" );
						
						if ( bValid ) {
							$("#commentForm")[0].submit();							 							
							$( this ).dialog( "close" );
						} 
					  }
				    },
				{ text: "取消",
				  click: function(){
					$( this ).dialog( "close" );
				  }
				}]
		});
	}
};

var videoForm = {
		movidId : "",
		videoId : "",
		allFields : null,
		
		strURLRegex : "^((https|http|ftp|rtsp|mms)?://)" 
	        + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@ 
	        + "(([0-9]{1,3}.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184 
	        + "|" // 允许IP和DOMAIN（域名）
	        + "([0-9a-z_!~*'()-]+.)*" // 域名- www. 
	        + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]." // 二级域名 
	        + "[a-z]{2,6})" // first level domain- .com or .museum 
	        + "(:[0-9]{1,4})?" // 端口- :80 
	        + "((/?)|" // a slash isn't required if there is no file name 
	        + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$",
	    	    	    
	    init:function(){
	    	videoForm.allFields = $( [] ).add($("#videoURL")).add($("#length")).add($("#provider"));
	    	
	    	$("#addVideo").bind("click", function() {
	    		videoForm.cleanVideoInfoForm();
	    		$("#addVideoDialog").dialog("open");
	        });
	    	
	    	$("#selectViedoSrc").bind("click", function() {		   
	    		$("#selectVideoSrcDialog").dialog("open");
	        });
	    	
	    	$("#selectVideoSrcDialog span").bind("click", function(){
	    		$("#provider").val(this.id);			
	    		$("#selectVideoSrcDialog").dialog("close");
	    	});
	    	
	    	$("#responsbility").bind("click", function(){
	    		if($("#responsbility").attr("checked") == "checked"){	    				    				    	
	    			$("#createVideoBtn").removeAttr("disabled").removeClass('ui-state-disabled');
	    		} else {	    			
	    			$("#createVideoBtn").attr("disabled","disabled").addClass('ui-state-disabled');	    			
	    		}	    			    		
	    	});
	    	
	    	$(".editVideo").bind("click", function(){
	    		videoForm.fillVideoInfoForm($(this));//load the data and fill out the form before editing 
	    		$("#addVideoDialog").dialog("open");    			    		
	    	});
	    	
            $(".delVideo").bind("click", function(){ 
            	videoForm.movidId = $(this).attr("movieId");
            	videoForm.videoId = $(this).attr("videoId")
            	$( "#deleteConfirm" ).dialog("open");            	            	            	            	
	    	});
	    		    	    
	    	$( "#selectVideoSrcDialog" ).dialog({
	    		autoOpen: false,
	    		height: 590,
	    		width: 630,
	    		modal: true
	    	});
	    	
	    	$( "#addVideoDialog" ).dialog({
				autoOpen: false,
				height: 530,
				width: 510,
				modal: true,
				
				close: function() {					
					$(".validateTips").text( "" ).removeClass( "ui-state-highlight" );						
					videoForm.allFields.removeClass( "ui-state-error" );			
				},
				
				buttons: [
				   { id: "createVideoBtn",
					 text: "创建",
					 disabled: "disabled",
					 click: function() {
				        var bValid = true;
						videoForm.allFields.removeClass( "ui-state-error" );
						bValid = bValid && videoForm.checkRegexp( $( "#provider" ), "^\\w+$", "请选择视频的来源" );
						//bValid = bValid && videoForm.checkRegexp( $( "#videoURL" ), videoForm.strURLRegex, "请输入合法的网站地址" );				
						bValid = bValid && videoForm.checkRegexp( $("#length"), "^[1-9]\\d{0,2}$", "请输入合适的视频长度"); //非0正整数
						
						if ( bValid ) {
							$("#videoForm")[0].submit();							 							
							$( this ).dialog( "close" );
						} 
					  }
				    },
					{ text: "取消",
					  click: function(){
						$( this ).dialog( "close" );
					  }
					}] 														
			});
	    	
	    	$( "#deleteConfirm" ).dialog({
	    		autoOpen:false,
				resizable: false,
				height:140,
				modal: true,
				buttons: {
					"删除": function() {
						$( this ).dialog( "close" );
						if (videoForm.movidId != "" && videoForm.videoId!="")
						  window.location="video!deleteVideo?video.movie.id="+ videoForm.movidId + "&video.id=" + videoForm.videoId;						
					},
					"取消": function() {
						$( this ).dialog( "close" );
					}
				}
			});
	    	
	    },
	   
	    updateTips : function( t ) {
	    	$(".validateTips").text( t ).addClass( "ui-state-highlight" );		
		},
		
		checkRegexp : function( o, regexp, n ) {			
			var re = new RegExp(regexp); 
			if ( !( re.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				videoForm.updateTips( n );
				return false;
			} else {
				return true;
			}
		},
		
		fillVideoInfoForm : function (video) {
			$("#videoId").val(video.attr("videoId"));
			$("#videoName").val(video.attr("name"));
			$("#provider").val(video.attr("provider"));
			$("#videoURL").val(video.attr("url"));
			$("#length").val(video.attr("length"));
			$("#quality").val(video.attr("quality"));
			
		},
		
		cleanVideoInfoForm :function(){
			$("#videoId").val("");
			$("#videoName").val("");
			$("#provider").val("");
			$("#videoURL").val("");
			$("#length").val("");
			$("#quality").val("");
		}
};

var plotForm = {
	allFields : null,
	
	init:function(){
		plotForm.allFields = $( [] ).add($("#moviePlot"));
		
		$("#editPlot").bind("click", function() {
    		$("#editPlotDialog").dialog("open");

        });
		
		$( "#editPlotDialog" ).dialog({
			autoOpen: false,			
			height: 320,
			width: 600,
			modal: true,
			close: function() {					
				$(".validateTips").text( "" ).removeClass( "ui-state-highlight" );						
				plotForm.allFields.removeClass( "ui-state-error" );			
			},
			
			buttons: [
			   { text: "保存",
				 click: function() {
			        var bValid = true;
			        plotForm.allFields.removeClass( "ui-state-error" );
					bValid = bValid && plotForm.checkLength( $( "#moviePlot" ), "请填写剧情，长度1-4000个字符。", 4000 );
					
					if ( bValid ) {
						$("#plotForm")[0].submit();							 							
						$( this ).dialog( "close" );
					} 
				  }
			    },
				{ text: "取消",
				  click: function(){
					$( this ).dialog( "close" );
				  }
				}] 														
		});
		
	},
	
	checkLength : function( o, n, len ) {			
		var aa = o.val();
		if ( aa.length==0 || aa.length >len ) {
			o.addClass( "ui-state-error" );
			videoForm.updateTips( n );
			return false;
		} else {
			return true;
		}
	}
};

var commentForm = {
	allFields : null,
	init:function(){
		commentForm.allFields = $( [] ).add($("#comment"));
		$("#addComment").bind("click", function() {
    		$("#addCommentDialog").dialog("open");

        });
		
		$( "#addCommentDialog" ).dialog({
			autoOpen: false,			
			height: 320,
			width: 600,
			modal: true,
			close: function() {					
				$(".validateTips").text( "" ).removeClass( "ui-state-highlight" );						
				commentForm.allFields.removeClass( "ui-state-error" );			
			},
			
			buttons: [
			   { text: "评论并转发到微博",
				 click: function() {
					$( "#sendToWeibo" ).val(true);
			        commentForm.allFields.removeClass( "ui-state-error" );
			        var bValid = plotForm.checkLength( $( "#comment" ), "请填写评论，长度1-140个字节（一个汉字占两个字节）。", 140 );
					if ( bValid ) {
						$("#commentForm")[0].submit();							 							
						$( this ).dialog( "close" );
					} 
				  }
			    },
			    { id: "createVideoBtn",
					 text: "评论",
					 click: function() {
						$( "#sendToWeibo" ).val(false);
				        commentForm.allFields.removeClass( "ui-state-error" );
				        var bValid = plotForm.checkLength( $( "#comment" ), "请填写评论，长度1-1000个字符。", 1000 );
						
						if ( bValid ) {
							$("#commentForm")[0].submit();							 							
							$( this ).dialog( "close" );
						} 
					  }
				    },
				{ text: "取消",
				  click: function(){
					$( this ).dialog( "close" );
				  }
				}]
		});
	}
};


var descriptionForm = {
	allFields : null,
	init:function(){
		descriptionForm.allFields = $( [] ).add($("#description"));
		$("#editDescription").bind("click", function() {
    		$("#editDescriptionDialog").dialog("open");
        });
		
		$( "#editDescriptionDialog" ).dialog({
			autoOpen: false,			
			height: 320,
			width: 600,
			modal: true,
			close: function() {					
				$(".validateTips").text( "" ).removeClass( "ui-state-highlight" );						
				descriptionForm.allFields.removeClass( "ui-state-error" );			
			},
			
			buttons: [
			    { 
					 text: "保存",
					 click: function() {
						//$( "#sendToWeibo" ).val(false);
						descriptionForm.allFields.removeClass( "ui-state-error" );
				        var bValid = plotForm.checkLength( $( "#description" ), "请填写简介，长度1-4000个字符。", 4000 );
						
						if ( bValid ) {
							$("#descriptionForm")[0].submit();							 							
							$( this ).dialog( "close" );
						}
					  }
				    },
				{ text: "取消",
				  click: function(){
					$( this ).dialog( "close" );
				  }
				}]
		});
	}
};